/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/util/util.c,v 1.10 2002/09/14 19:37:43 bboy Exp $
	util.c: Routines shared by the utility programs.

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "util.h"

CONF *Conf;															/* Configuration data */
MYSQL *mysql;														/* MySQL connection */


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	LOAD_CONFIG
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
load_config(void)
{
	conf_load(&Conf, MYDNS_CONF);

	/* Set table names if provided */
	mydns_set_soa_table_name(conf_get(&Conf, "soa-table"));
	mydns_set_rr_table_name(conf_get(&Conf, "rr-table"));
	mydns_set_ptr_table_name(conf_get(&Conf, "ptr-table"));
}
/*--- load_config() -----------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	DB_CONNECT
	Connect to the database.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
db_connect(void)
{
	u_char *user = conf_get(&Conf, "mysql-user"), *portp = NULL;
	u_char *host = conf_get(&Conf, "mysql-host");
	u_char *database = conf_get(&Conf, "database");
	u_char *password = conf_get(&Conf, "mysql-password");
	unsigned int port = 0;

	if (host && (portp = strchr(host, ':')))
	{
		port = atoi(portp + 1);
		*portp = '\0';
	}

	if (!(mysql = mysql_init(NULL)))
		Err(_("unable to allocate MySQL data structure"));

	if (!(mysql_real_connect(mysql, host, user, password, database, port, NULL, 0)))
		ErrSQL(mysql, _("error connecting to MySQL server at %s"), host);

	if (portp)
		*portp = ':';
	Verbose(_("connected to MySQL on %s, db \"%s\""), host, database);
}
/*--- db_connect() ------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	SQLNUM
	Issues a query and returns the first column of the first row as a number.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
u_int32_t
sqlnum(const char *fmt, ...)
{
	va_list ap;
	char query[BUFSIZ];
	MYSQL_RES *res = NULL;
	MYSQL_ROW row;
	u_int32_t rv = 0;

	va_start(ap, fmt);
	vsnprintf(query, sizeof(query), fmt, ap);
	va_end(ap);

	if (mysql_real_query(mysql, query, strlen(query)) || !(res = mysql_store_result(mysql)))
		ErrSQL(mysql, _("error issuing query"));

	/* Add results to list */
	if ((row = mysql_fetch_row(res)) && row[0])
		rv = atou(row[0]);
	mysql_free_result(res);
	return (rv);
}
/*--- sqlnum() ----------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	METER
	Outputs a very simple progress meter - only used if stderr is a terminal.
	If "total" is zero, the meter is erased.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline void
meter(unsigned long current, unsigned long total)
{
	char m[80];
	int num;

	if (!isatty(STDERR_FILENO))
		return;

	if (!total || current > total)							/* Erase meter */
	{
		memset(m, ' ', 73);
		m[72] = '\r';
		fwrite(m, 73, 1, stderr);
		fflush(stderr);
		return;
	}

	/* Create meter */
	memset(m, '.', 63);
	m[0] = m[60] = '|';
	m[5] = m[10] = m[15] = m[20] = m[25] = m[30] = m[35] = m[40] = m[45] = m[50] = m[55] = ':';
	m[61] = '\r';
	m[62] = '\0';

	num = ((double)((double)current / (double)total) * 58.0) + 1;
	memset(m + 1, '#', num < 0 || num > 58 ? 58 : num);
	fprintf(stderr, "  %6.2f%% %s\r", PCT(total,current), m);
}
/*--- meter() -----------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	TAI64TIME
	Converts a "TAI64" timestamp to a Unix timestamp.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
time_t
tai64time(char *str)
{
	u_int64_t secs = 0;
	unsigned long nanosecs = 0UL;
	unsigned long u = 0UL;
	register int n;

	for (n = 1; n < strlen(str); n++)
	{
		u = str[n] - '0';
		if (u >= 10)
		{
			u = str[n] - 'a';
			if (u >= 6)
				break;
			u += 10;
		}
		secs <<= 4;
		secs += nanosecs >> 28;
		nanosecs &= 0xfffffff;
		nanosecs <<= 4;
		nanosecs += u;
	}
	secs -= 4611686018427387914ULL;
	return ((time_t)secs);
}
/*--- tai64time() -------------------------------------------------------------------------------*/ 

/* vi:set ts=3: */
/* NEED_PO */
